package com.hskn.hss.module.worklog.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.controller.BaseController;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.core.utils.poi.ExcelUtil;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.costdictitem.service.ICostDictItemService;
import com.hskn.hss.module.depart.service.ISysDepartService;
import com.hskn.hss.module.tlworktype.entity.TlWorkType;
import com.hskn.hss.module.videoplan.mapper.VideoPlanMapper;
import com.hskn.hss.module.worklog.entity.WorkLog;
import com.hskn.hss.module.worklog.mapper.WorkLogMapper;
import com.hskn.hss.module.worklog.service.IWorkLogService;
import com.hskn.hss.module.worklog.to.AncientGardenTreesTO;
import com.hskn.hss.module.worklog.to.DeleteBatchTO;
import com.hskn.hss.module.worklog.to.JobLogExportTO;
import com.hskn.hss.module.worklog.vo.*;
import com.hskn.hss.utils.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-05-19
 */
@Slf4j
@RestController
@RequestMapping("/hss/worklog")
@Api(tags = {"-work_log"}, description = "作业日志")
public class WorkLogController extends BaseController {

    @Resource
    WorkLogMapper workLogMapper;
    @Autowired
    private IWorkLogService workLogService;
    @Autowired
    private ISysDepartService departService;
    @Autowired
    private ICostDictItemService costDictItemService;

    @RequestMapping(value = "list", method = RequestMethod.POST)
    public AjaxResult<Object> list(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = workLogService.mList(params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "listById", method = RequestMethod.POST)
    public AjaxResult listById(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            if (params != null && params.containsKey("pageNum") && params.containsKey("pageSize")) {
                startPage(params.getInteger("pageNum"), params.getInteger("pageSize"));
            } else {
                return AjaxResult.fail("pageNum，pageSize不能为空");
            }
            return AjaxResult.success(workLogService.listById(params));
        } catch (HssException e) {
            return AjaxResult.fail();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail();
        }
    }

    @RequestMapping(value = "rList", method = RequestMethod.POST)
    public AjaxResult<Object> rList(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = workLogService.rList(params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public AjaxResult<Object> add(@RequestBody WorkLog workLog) {
        try {
            workLogService.add(workLog);
            return AjaxResult.success("发布成功");
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.UPDATE, tableName = "work_log", idKey = "workLogId", bean = WorkLog.class, beanMapper = WorkLogMapper.class, names = "title")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public AjaxResult<Object> update(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            workLogService.mUpdate(params);
            return AjaxResult.success("修改成功");
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.DELETE, tableName = "work_log", idKey = "workLogId", bean = WorkLog.class, beanMapper = WorkLogMapper.class, names = "title")
    @RequestMapping(value = "del", method = RequestMethod.POST)
    public AjaxResult<Object> del(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            workLogService.del(params);
            return AjaxResult.success("删除成功");
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.DELETE, tableName = "work_log", idKey = "ids", bean = WorkLog.class, beanMapper = WorkLogMapper.class, names = "title")
    @ApiOperation(value = "作业日志批量删除", notes = "作业日志批量删除")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok")
    })
    @PostMapping(value = "deleteBatch")
    public AjaxResult deleteBatch(@RequestBody @Validated DeleteBatchTO deleteBatchTO) {
        try {
            if (CollectionUtils.isNotEmpty(deleteBatchTO.getIds())) {
                return toAjax(workLogService.removeByIds(deleteBatchTO.getIds()));
            } else {
                return AjaxResult.fail("请选中后重试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * app撤销日志
     *
     * @param request
     * @return
     */
    @SysLog(businessType = BusinessType.UPDATE, tableName = "work_log", idKey = "workLogId", bean = WorkLog.class, beanMapper = WorkLogMapper.class, names = "title")
    @RequestMapping(value = "revoke", method = RequestMethod.POST)
    public AjaxResult<Object> revoke(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            workLogService.revoke(params);
            return AjaxResult.success("撤销成功");
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 车辆作业日志
     *
     * @param jobLogExportTO
     * @return com.hskn.hss.entity.AjaxResult
     * @author zcy
     * @date 2021/11/8 19:48
     */
    @ApiOperation(value = "车辆作业日志", notes = "车辆作业日志")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = JobLogExportCarExcelVO.class)
    })
    @PostMapping(value = "selectCarWorkLogList")
    public AjaxResult selectCarWorkLogList(@RequestBody @Validated JobLogExportTO jobLogExportTO) {
        try {
            return AjaxResult.success(Tools.buildTableDataInfo(jobLogExportTO.getPage(), jobLogExportTO.getSize(),workLogService.selectCarWorkLogList(jobLogExportTO)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 人员作业日志
     *
     * @param jobLogExportTO
     * @return com.hskn.hss.entity.AjaxResult
     * @author zcy
     * @date 2021/11/8 19:48
     */
    @ApiOperation(value = "人员作业日志", notes = "人员作业日志")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = JobLogExportPersonExcelVO.class)
    })
    @PostMapping(value = "selectPersonWorkLogList")
    public AjaxResult selectPersonWorkLogList(@RequestBody @Validated JobLogExportTO jobLogExportTO) {
        try {
            return AjaxResult.success(Tools.buildTableDataInfo(jobLogExportTO.getPage(), jobLogExportTO.getSize(),workLogService.selectPersonWorkLogList(jobLogExportTO)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 设施作业日志
     *
     * @param jobLogExportTO
     * @return com.hskn.hss.entity.AjaxResult
     * @author zcy
     * @date 2021/11/8 19:48
     */
    @ApiOperation(value = "设施作业日志", notes = "设施作业日志")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = JobLogExportFacExcelVO.class)
    })
    @PostMapping(value = "selectFacWorkLogList")
    public AjaxResult selectFacWorkLogList(@RequestBody @Validated JobLogExportTO jobLogExportTO) {
        try {
            return AjaxResult.success(Tools.buildTableDataInfo(jobLogExportTO.getPage(), jobLogExportTO.getSize(),workLogService.selectFacWorkLogList(jobLogExportTO)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 园林作业日志
     *
     * @param jobLogExportTO
     * @return com.hskn.hss.entity.AjaxResult
     * @author zcy
     * @date 2021/11/8 19:48
     */
    @ApiOperation(value = "园林作业日志", notes = "园林作业日志")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = JobLogExportGardenExcelVO.class)
    })
    @PostMapping(value = "selectGarWorkLogList")
    public AjaxResult selectGarWorkLogList(@RequestBody @Validated AncientGardenTreesTO jobLogExportTO) {
        try {
            return AjaxResult.success(Tools.buildTableDataInfo(jobLogExportTO.getPage(), jobLogExportTO.getSize(),workLogService.selectGarWorkLogList(jobLogExportTO)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 古树作业日志
     *
     * @param jobLogExportTO
     * @return com.hskn.hss.entity.AjaxResult
     * @author zcy
     * @date 2021/11/8 19:48
     */
    @ApiOperation(value = "古树作业日志", notes = "古树作业日志")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = JobLogExportTreeExcelVO.class)
    })
    @PostMapping(value = "selectTreeWorkLogList")
    public AjaxResult selectTreeWorkLogList(@RequestBody @Validated AncientGardenTreesTO jobLogExportTO) {
        try {
            return AjaxResult.success(Tools.buildTableDataInfo(jobLogExportTO.getPage(), jobLogExportTO.getSize(),workLogService.selectTreeWorkLogList(jobLogExportTO)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 车辆作业日志导出
     *
     * @param jobLogExportTO
     * @return
     */
    @ApiOperation(value = "车辆作业日志导出接口", notes = "车辆作业日志导出")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = JobLogExportCarExcelVO.class)
    })
    @PostMapping("carWorkLogExcelExport")
    public com.hskn.hss.core.domain.AjaxResult carWorkLogExcelExport(@RequestBody @Validated JobLogExportTO jobLogExportTO) {
        try {
            List<JobLogExportCarExcelVO> jobLogExportCarExcelVOList = workLogService.selectCarWorkLogList(jobLogExportTO);
            // 工具类实例化
            ExcelUtil<JobLogExportCarExcelVO> util = new ExcelUtil<JobLogExportCarExcelVO>(JobLogExportCarExcelVO.class);
            // 导出
            return util.exportExcel(jobLogExportCarExcelVOList, "车辆作业日志");

        } catch (Exception e) {
            e.printStackTrace();
            return com.hskn.hss.core.domain.AjaxResult.error();
        }

    }

    /**
     * 人员作业日志导出
     *
     * @param jobLogExportTO
     * @return
     */
    @ApiOperation(value = "人员作业日志导出接口", notes = "人员作业日志导出")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = JobLogExportPersonExcelVO.class)
    })
    @PostMapping("empWorkLogExcelExport")
    public com.hskn.hss.core.domain.AjaxResult empWorkLogExcelExport(@Validated @RequestBody JobLogExportTO jobLogExportTO) {
        try {
            List<JobLogExportPersonExcelVO> jobLogExportPersonExcelVOList = workLogService.selectPersonWorkLogList(jobLogExportTO);
            //工具类实例化
            ExcelUtil<JobLogExportPersonExcelVO> util = new ExcelUtil<JobLogExportPersonExcelVO>(JobLogExportPersonExcelVO.class);
            //            导出
            return util.exportExcel(jobLogExportPersonExcelVOList, "人员作业日志");

        } catch (Exception e) {
            e.printStackTrace();
            return com.hskn.hss.core.domain.AjaxResult.error();
        }

    }

    /**
     * 设施作业日志导出
     *
     * @param jobLogExportTO
     * @return
     */
    @ApiOperation(value = "设施作业日志导出", notes = "设施作业日志导出")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = JobLogExportFacExcelVO.class)
    })
    @PostMapping("facWorkLogExcelExport")
    public com.hskn.hss.core.domain.AjaxResult facWorkLogExcelExport(@Validated @RequestBody JobLogExportTO jobLogExportTO) {
        try {
            List<JobLogExportFacExcelVO> jobLogExportFacExcelVOList = workLogService.selectFacWorkLogList(jobLogExportTO);
            //工具类实例化
            ExcelUtil<JobLogExportFacExcelVO> util = new ExcelUtil<JobLogExportFacExcelVO>(JobLogExportFacExcelVO.class);
            //            导出
            return util.exportExcel(jobLogExportFacExcelVOList, "设施作业日志");

        } catch (Exception e) {
            e.printStackTrace();
            return com.hskn.hss.core.domain.AjaxResult.error();
        }

    }

    /**
     * 园林作业日志导出
     *
     * @param jobLogExportTO
     * @return
     */
    @ApiOperation(value = "园林作业日志导出", notes = "园林作业日志导出")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = JobLogExportGardenExcelVO.class)
    })
    @PostMapping("garWorkLogExcelExport")
    public com.hskn.hss.core.domain.AjaxResult garWorkLogExcelExport(@Validated @RequestBody AncientGardenTreesTO jobLogExportTO) {
        try {
            List<JobLogExportGardenExcelVO> jobLogExportGardenExcelVOList = workLogService.selectGarWorkLogList(jobLogExportTO);
            //工具类实例化
            ExcelUtil<JobLogExportGardenExcelVO> util = new ExcelUtil<JobLogExportGardenExcelVO>(JobLogExportGardenExcelVO.class);
            //导出
            return util.exportExcel(jobLogExportGardenExcelVOList, "园林作业日志");

        } catch (Exception e) {
            e.printStackTrace();
            return com.hskn.hss.core.domain.AjaxResult.error();
        }

    }

    /**
     * 古树作业日志导出
     *
     * @param jobLogExportTO
     * @return
     */
    @ApiOperation(value = "古树作业日志导出", notes = "古树作业日志导出")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = JobLogExportTreeExcelVO.class)
    })
    @PostMapping("treeWorkLogExcelExport")
    public com.hskn.hss.core.domain.AjaxResult treeWorkLogExcelExport(@Validated @RequestBody AncientGardenTreesTO jobLogExportTO) {
        try {
            List<JobLogExportTreeExcelVO> JobLogExportTreeExcelVOList = workLogService.selectTreeWorkLogList(jobLogExportTO);
            //工具类实例化
            ExcelUtil<JobLogExportTreeExcelVO> util = new ExcelUtil<JobLogExportTreeExcelVO>(JobLogExportTreeExcelVO.class);
            //            导出
            return util.exportExcel(JobLogExportTreeExcelVOList, "古树作业日志");

        } catch (Exception e) {
            e.printStackTrace();
            return com.hskn.hss.core.domain.AjaxResult.error();
        }

    }

}

